package com.neuedu.mblog.dao;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;

/**
 * 项目：web04
 * 创建时间：  2021/12/3   8:34
 * 作者 :jshand
 * 描述 : 所有Dao的 父类
 */
public abstract class BaseDao<T> {

    protected static DruidDataSource dataSource = null;

    protected QueryRunner run ;

    public BaseDao() {
        run  = new QueryRunner( dataSource );
    }

    private static Properties loadJdbcProperties(){
        InputStream  is = BaseDao.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return properties;
    }
    //全局只执行一次，
    static {


        dataSource = new DruidDataSource();


        //数据源连接池
//        dataSource.setUsername("root");
//        dataSource.setPassword("root");
//        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/mto_blog");
//        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//        dataSource.setInitialSize(8);

        //根据jdbc.properties 文件加载数据源信息
        dataSource.configFromPropety(loadJdbcProperties());
    }


    //旧代码使用
    public Connection getConnection() throws  SQLException {
        return dataSource.getConnection();
    }

    public void close(Connection con , Statement stmt ){
       if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void close(Connection con , Statement stmt , ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    
    
    public BigInteger getPK() throws SQLException {
        //获取主键
        String keySql ="SELECT   @@identity";
        BigInteger key = (BigInteger) run.query(keySql,new ScalarHandler(1));
        return key;
    }




    public long count(String sql ,Object ...params) throws SQLException {

        Long count = (Long) run.query(sql,new ScalarHandler(1),params);

        return count;
    }

    public long total(String sql ,Object ...params) throws SQLException {
        String countSql = " select count(1) c from ("+sql+")  AS tab";
       return this.count(countSql,params);
    }

    //定义一下 每个Dao都应该有点 规范
    //查集合
    public abstract  List<T> selectList(String sql) throws Exception;
    public abstract  List<T> selectList(String sql,Object ... params) throws Exception;
    //查一个
    public abstract <E> E selectById(Integer id)throws Exception;
    //保存
    public abstract int save(T t)throws Exception;
    //更新
    public abstract int update(T t)throws Exception;
    //删除
    public abstract int delete(Integer id)throws Exception;
}
